
//
//  TSUserViewController.m
//  chart
//
//  Created by xu on 15/8/16.
//  Copyright (c) 2015年 xu. All rights reserved.
//

#import "TSUserViewController.h"
#import "TSUserListTableView.h"
#import "TSCreateScheduleViewController.h"
#import "TSRecycleMsgListViewController.h"
@interface TSUserViewController ()

@end

@implementation TSUserViewController{
    TSUserListTableView *tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshUserViewController:) name:TSNotification_ScheduleCreateSucceeded object:nil];
    
    self.navigationItem.title = @"个人日程";
    
    WS(weakSelf);
    [self rightBarButtonWithTitle:nil barNorImage:[UIImage imageNamed:@"topic_follow_add"] barHltImage:nil action:^{
        //[weakSelf onRightBarBarButtonClick];
        if (!weakSelf) {
            return;
        }
        UIActionSheet *action_sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:weakSelf cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"创建个人日程",@"回收站", nil];
        [action_sheet showInView:weakSelf.view];
    }];
    
    if (self.userId) {
        [self leftBarButtonWithTitle:nil barNorImage:nil barHltImage:nil action:nil];
        
        [self rightBarButtonWithTitle:nil barNorImage:[UIImage imageNamed:@"nav_dark_close"] barHltImage:nil action:^{
            [weakSelf onRightBarBarButtonClick];
        }];
    }
    
    tableView = [[TSUserListTableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain WithId:self.userId];
    tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:tableView];
}
#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        [self onRightBarBarButtonClick];
    }else if (buttonIndex == 1) {
        [self showRecycle];
    }
}

-(void)showRecycle
{
    TSRecycleMsgListViewController *msg_vc = [[TSRecycleMsgListViewController alloc] init];
    msg_vc.isSchedule =YES;
    TSBaseNavigationController *nav_vc = [[TSBaseNavigationController alloc] initWithRootViewController:msg_vc];
    [self presentViewController:nav_vc animated:YES completion:nil];
}
- (void)refreshUserViewController:(NSNotification *)noti
{
    if (tableView) {
        [tableView.header beginRefreshing];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)onRightBarBarButtonClick
{
    if (self.userId) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    else{
        TSCreateScheduleViewController *schedule_vc = [[TSCreateScheduleViewController alloc] init];
        TSBaseNavigationController *nav = [[TSBaseNavigationController alloc] initWithRootViewController:schedule_vc];
        [self presentViewController:nav animated:YES completion:nil];
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}




@end
